package com.example.transaction.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.transaction.sys.entity.Address;
import com.example.transaction.sys.entity.Chat;
import com.example.transaction.sys.entity.User;
import com.example.transaction.sys.mapper.AddressMapper;
import com.example.transaction.sys.mapper.ChatMapper;
import com.example.transaction.sys.mapper.UserMapper;
import com.example.transaction.sys.service.IAddressService;
import com.example.transaction.sys.service.IChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat> implements IChatService {

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CacheManager cacheManager;

    @Cacheable(value = "chats", key = "#userID+'and'+#targetUserID")
    @Override
    public List<Chat> getChatHistory(Integer userID, Integer targetUserID) {
        LambdaQueryWrapper<Chat> wrapper = new LambdaQueryWrapper<>();
        wrapper.and(q -> q.eq(Chat::getUserID, userID).eq(Chat::getTargetUserID, targetUserID)
                        .or().eq(Chat::getUserID, targetUserID).eq(Chat::getTargetUserName, userID))
                .orderByAsc(Chat::getCreateTime);
        return chatMapper.selectList(wrapper);
    }

//    @CacheEvict(value = "chats",key = "#userID+'and'+#targetUserID")
//    @CacheEvict(value = "chats",key = "'chatUser'+#userID")
    @Override
    public Chat saveChat(Integer userID, Integer targetUserID, String userName, String targetUserName, String content) {
        // 手动删除缓存条目
        cacheManager.getCache("chats").evict("chatUser" + userID);
        cacheManager.getCache("chats").evict(userID + "and" + targetUserID);
        cacheManager.getCache("chats").evict("chatUser" + targetUserID);
        cacheManager.getCache("chats").evict(targetUserID + "and" + userID);
        Chat chat = new Chat();
        chat.setUserID(userID);
        chat.setTargetUserID(targetUserID);
        chat.setUserName(userName);
        chat.setTargetUserName(targetUserName);
        chat.setContent(content);
        chat.setCreateTime(LocalDateTime.now());
        chatMapper.insert(chat);
        return chat;
    }

    @Cacheable(value = "chats", key = "'chatUser'+#userID")
    @Override
    public List<User> getChatUsers(Integer userID) {
        // 查询所有与userID有过聊天的唯一用户ID
        List<Integer> chatUserIDs = chatMapper.findDistinctChatUserIDs(userID);

        // 去除重复的用户ID
        Set<Integer> uniqueUserIDs = new HashSet<>(chatUserIDs);

        // 查询这些唯一用户ID对应的用户信息
        List<User> chatUsers = new ArrayList<>();
        for (Integer id : uniqueUserIDs) {
            User user = userMapper.selectById(id);
            if (user != null) {
                chatUsers.add(user);
            }
        }
        return chatUsers;
    }
}
